﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using viForum.Classes;
using viForum.Controllers;
using visinia.SmartEngine.Modules;
using visinia.SmartEngine;

public partial class Modules_viForum_Forums : WebModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //set forum theme
        string themeCSS = WebRoots.GetModulesRoot + "viForum/Themes/" + BaseController.ForumTheme + "/viForum.css";
        WebPageController.AddCssLink(Page, themeCSS, true);
        BindForums();
    }

    private void BindForums()
    {
        List<CategoryData> forumdataList = new List<CategoryData>();
        List<Category> categories = CategoryController.GetCategories();
        foreach (Category category in categories)
        {
            CategoryData data = new CategoryData();
            data.ForumCategory = category.Title;
            List<Forum> forums = ForumController.GetForums(category);
            foreach (Forum forum in forums)
            {
                ForumData forumData = new ForumData();
                forumData.ForumTitle = forum.Title;
                forumData.ForumDescription = forum.Description;
                forumData.ForumUrl = forum.Url;
                forumData.ForumThreadCount = ThreadController.GetThreadCount(forum.ForumId);
                forumData.ForumPostCount = ForumController.GetPostCount(forum.ForumId);
                Thread thread = ThreadController.GetLatestThread(forum.ForumId);
                if (thread == null)
                    forumData.ForumLatestPost = "";
                else
                {
                    string LatestPost = "<a href=\"" + thread.Url + "\" >" + TrimContent(thread.Content) + "</a> <br/><em> Posted by " + thread.UserName + "<br/>on " + thread.Date.ToString() + "</em>";
                    forumData.ForumLatestPost = LatestPost;
                }
                data.forumData.Add(forumData);
            }
            forumdataList.Add(data);
        }
        ForumsCategoryList.DataSource = forumdataList;
        ForumsCategoryList.DataBind();

    }

    private string TrimContent(string content)
    {
        if (content.Length > 50)
            return WebHelpers.StripHtml(content.Remove(50)) + "...";
        return WebHelpers.StripHtml(content);
    }

    public class CategoryData
    {
        private string _ForumCategory;
        public string ForumCategory { get { return _ForumCategory; } set { _ForumCategory = value; } }
        public List<ForumData> forumData = new List<ForumData>();
    }

    public class ForumData
    {    
        private string _ForumUrl;
        public string ForumUrl { get { return _ForumUrl; } set { _ForumUrl = value; } }

        private string _ForumTitle;
        public string ForumTitle { get { return _ForumTitle; } set { _ForumTitle = value; } }

        private string _ForumDescription;
        public string ForumDescription { get { return _ForumDescription; } set { _ForumDescription = value; } }

        private int _ForumThreadCount;
        public int ForumThreadCount { get { return _ForumThreadCount; } set { _ForumThreadCount = value; } }

        private int _ForumPostCount;
        public int ForumPostCount { get { return _ForumPostCount; } set { _ForumPostCount = value; } }

        private string _ForumLatestPost;
        public string ForumLatestPost { get { return _ForumLatestPost; } set { _ForumLatestPost = value; } }
    }

    protected void ForumsCategoryList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        Repeater forumList = (Repeater)e.Item.FindControl("ForumsList");
        if (forumList != null)
        {
            CategoryData data = (CategoryData)e.Item.DataItem;
            forumList.DataSource = data.forumData;
            forumList.DataBind();
        }        
    }
}
